package com.itheima.controller;

//部门控制层：接受请求，调用service层方法，返回响应数据

import com.itheima.Aspect.LogOperation;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController//标记这是一个控制层
public class DeptController {


    @Autowired
    private DeptService deptService;


    @RequestMapping("/depts")//接受请求
    public Result list(){
        //调用业务
        List<Dept> deptLisst = deptService.findAll();
        //返回数据
        return Result.success(deptLisst);
    }

    @LogOperation
    @DeleteMapping("/depts")
    public Result delete(Integer id){
        System.out.println("根据id删除部门,id=" + id);
        deptService.deleteById(id);
        return Result.success();
    }

    @LogOperation
    @PostMapping("/depts")
    public Result save(@RequestBody Dept dept){
        System.out.println("新增部门，dept" + dept);
        deptService.save(dept);
        return Result.success();
    }

    @GetMapping("/depts/{id}")
    public Result getById(@PathVariable Integer id){
        System.out.println("根据ID查询,id = " + id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    @LogOperation
    @PutMapping("/depts")
    public Result update(@RequestBody Dept dept){
        System.out.println("修改部门, dept = " + dept);
        deptService.update(dept);
        return Result.success();
    }
}
